/*
 * @lc app=leetcode.cn id=8 lang=golang
 *
 * [8] 字符串转换整数 (atoi)
 */

// @lc code=start
func myAtoi(s string) int {
	var navigate, start bool
	var n int = 0
	for _, ch := range s {
		if start {
			if ch >= 48 && ch <= 57 {
				n = int(ch) - 48 + n*10
				if n > 0x80000000 {
					break
				}
				continue
			}
			break
		}
		if ch >= 48 && ch <= 57 {
			n = int(ch) - 48
			start = true
			continue
		}
		if ch == ' ' {
			continue
		}
		if ch == '-' {
			navigate = true
			start = true
			continue
		}
		if ch == '+' {
			start = true
			continue
		}
		break
	}
	if navigate {
		n = -n
		if n < -0x80000000 {
			return -0x80000000
		}
	} else {
		if n > 0x7FFFFFFF {
			return 0x7FFFFFFF
		}
	}
	return n
}

// @lc code=end

